import Vue from 'vue'
import Router from 'vue-router'
import Layout from '../view/Layout/'
Vue.use(Router)

const router = new Router({
  // 使用前端路由，当切换到新路由时，想要页面滚到顶部，或者是保持原先的滚动位置，就像重新加载页面那样。
  scrollBehavior(to, form, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  routes: [
    {
      path: '/login',
      component: () => import('../view/Login/login.vue'),
      hidden: true
    },
    {
      path: '/404',
      component: () => import('../view/404/404.vue'),
      hidden: true
    },
    {
      path: '/home',
      component: Layout,
      name: '首页',
      children: [
        {
          path: '/home',
          component: () => import('../view/Home/home.vue'),
          meta: { title: '首页', icon: 'fa fa-home' }
        }
      ],
      meta: {
        title: '首页',
        requiresAuth: true
      }
    },
    {
      path: '/example',
      redirect: '/example/table',
      component: Layout,
      name: '用例页面',
      meta: {
        requiresAuth: true,
        title: '用例页面',
        icon: 'fa fa-puzzle-piece'
      },
      children: [
        {
          path: '/example/table',
          component: () => import('../view/Example/Table/table.vue'),
          name: '表格',
          meta: { title: 'Table', icon: 'table' }
        },
        {
          path: '/example/form',
          component: () => import('../view/Example/Form/form.vue'),
          name: '表单',
          meta: { title: 'Form', icon: 'form' }
        }
      ]
    },
    {
      path: '/menu',
      component: Layout,
      redirect: '/menu/menu1',
      name: ' 无限层级',
      children: [
        {
          path: '/menu/menu1',
          component: () => import('../view/Example/MenuExample/'),
          name: '层级1',
          meta: { title: '层级1' },
          children: [
            {
              path: '/menu/menu1/menu1-1',
              component: () => import('../view/Example/MenuExample/Menu1'),
              name: '层级1-1',
              meta: { title: '层级1-1' }
            },
            {
              path: '/menu/menu1/menu1-2',
              component: () => import('../view/Example/MenuExample/Menu2'),
              name: '层级1-2',
              meta: { title: '层级1-2' }
            }
          ]
        },
        {
          path: '/menu/menu2',
          component: () => import('../view/Example/MenuExample/Menu2'),
          name: '层级2',
          meta: { title: '层级2' }
        }
      ],
      meta: {
        title: '无限层级',
        icon: 'fa fa-list',
        requiresAuth: true
      }
    },
    {
      path: '/',
      component: () => import('../view/Dashboard/dashboard.vue'),
      name: '大屏统计台',
      meta: {
        requiresAuth: true
      }
    },
    {
      // 404页面要在最后加载
      path: '*',
      redirect: '/404',
      hidden: true
    }
  ]
})

export default router
